package com.lucy.common.domain;

import cn.hutool.core.lang.Validator;
import com.lucy.common.constant.HttpStatus;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

@Data
@ApiModel(value = "响应数据封装")
public class ResponseDTO<T> {

    @ApiModelProperty(value = "响应状态码")
    protected Integer code;
    @ApiModelProperty(value = "响应携带信息")
    protected String msg;
    @ApiModelProperty(value = "响应状态标识")
    protected Boolean success;
    @ApiModelProperty(value = "响应数据")
    protected T data;

    public ResponseDTO() {

    }


    public ResponseDTO(HttpStatus httpStatus,String msg){
        this.code = httpStatus.value();
        this.msg = httpStatus.getReasonPhrase();
        if (Validator.isNotEmpty(msg)){
            this.msg = msg;
        }
        this.success = httpStatus.is2xxSuccessful();
    }

    public ResponseDTO(HttpStatus httpStatus){
        this.code = httpStatus.value();
        this.msg = httpStatus.getReasonPhrase();
        this.success = httpStatus.is2xxSuccessful();
    }

    public ResponseDTO(HttpStatus httpStatus,T data){
        this.code = httpStatus.value();
        this.msg = httpStatus.getReasonPhrase();
        this.data = data;
        this.success = httpStatus.is2xxSuccessful();
    }


    public ResponseDTO(HttpStatus httpStatus, T data, String msg) {
        super();
        this.code = httpStatus.value();
        this.msg = httpStatus.getReasonPhrase();
        if (Validator.isNotEmpty(msg)){
            this.msg = msg;
        }
        this.data = data;
        this.success = httpStatus.is2xxSuccessful();
    }


    public static <T> ResponseDTO<T> succ() {
        return new ResponseDTO<T>(HttpStatus.SUCCESS);
    }


    public static <T> ResponseDTO<T> succData(T data, String msg) {
        return new ResponseDTO<T>(HttpStatus.SUCCESS, data, msg);
    }

    public static <T> ResponseDTO<T> succData(T data) {
        return new ResponseDTO<T>(HttpStatus.SUCCESS, data);
    }

    public static <T> ResponseDTO<T> succMsg(String msg) {
        return new ResponseDTO<T>(HttpStatus.SUCCESS, msg);
    }


    public static <T> ResponseDTO<T> wrap(HttpStatus status) {
        return new ResponseDTO<>(status);
    }

    public static <T> ResponseDTO<T> wrap(HttpStatus status, T t) {
        return new ResponseDTO<T>(status, t);
    }

    public static <T> ResponseDTO<T> wrap(HttpStatus status, String msg) {
        return new ResponseDTO<T>(status, msg);
    }

    public static <T> ResponseDTO<T> wrapMsg(HttpStatus status, String msg) {
        return new ResponseDTO<T>(status, msg);
    }

    public String getMsg() {
        return msg;
    }

    public ResponseDTO<T> setMsg(String msg) {
        this.msg = msg;
        return this;
    }

    public int getCode() {
        return code;
    }

    public ResponseDTO<T> setCode(Integer code) {
        this.code = code;
        return this;
    }

    public T getData() {
        return data;
    }

    public ResponseDTO<T> setData(T data) {
        this.data = data;
        return this;
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    @Override
    public String toString() {
        return "ResponseDTO{" + "code=" + code + ", msg='" + msg + '\'' + ", success=" + success + ", data=" + data +
                '}';
    }
}
